<?php

/**
 * This is the model class for table "contact".
 *
 * The followings are the available columns in table 'contact':
 * @property integer $con_code
 * @property string $con_to
 * @property string $con_from
 * @property integer $pag_code
 *
 * The followings are the available model relations:
 * @property Page $pagCode
 * @property Field[] $fields
 */
class Contact extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Contact the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'contact';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pag_code', 'required'),
			array('pag_code', 'numerical', 'integerOnly'=>true),
			array('con_to, con_from', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('con_code, con_to, con_from, pag_code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pagCode' => array(self::BELONGS_TO, 'Page', 'pag_code'),
			'fields' => array(self::HAS_MANY, 'Field', 'con_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'con_code' => 'Con Code',
			'con_to' => 'Con To',
			'con_from' => 'Con From',
			'pag_code' => 'Pag Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('con_code',$this->con_code);
		$criteria->compare('con_to',$this->con_to,true);
		$criteria->compare('con_from',$this->con_from,true);
		$criteria->compare('pag_code',$this->pag_code);

    $page = (int)$_GET['page'];
    
    if($page > 0){
          $criteria->condition = 'pag_code='.$page;
    }else{
          throw new CHttpException(404, 'The requested page does not exist.');
    }
      
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}